﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Liberty.Domain.ServiceMemberManagement;
using Liberty.Domain.GrantManagement;

using Peradigm.Framework.Domain.Parties;
using Peradigm.Framework.UI.Web;

namespace Liberty.UI.GrantManagement.Controls
{
    [Serializable]
    public partial class GrantDetailsView : AbstractLibertyControl
    {
        #region Constants
        private const string
             SERVICE_MEMBER_URL = "~/ServiceMemberManagement/ServiceMemberView.aspx";
        #endregion

        #region Event Handlers
        protected void Page_Load( object sender, EventArgs e )
        {
        }
        protected void cmdSelectServiceMember_OnClick( object sender, EventArgs e )
        {
            Response.Redirect( SERVICE_MEMBER_URL );
        }
        #endregion

        #region Methods
        public override void UpdateView()
        {
            base.UpdateView();
            Grant grant = this.Grant;
            
            this.lnkServiceMemberRankAndName.Text = grant.Case.ServiceMember.CanonicalName;
            DateTime bornOn = DateTime.Parse( grant.Case.ServiceMember.BornOn.ToString() );
            this.lblServiceMemberDateOfBirth.Text = bornOn.ToString( "d MMMM, yyyy" );

            this.lblGrantNumber.Text = grant.Oid.ToString();
            this.lblGrantType.Text = grant.Type.DisplayString;
            //this.lblCreatedBy.Text = grant.
            this.lblDateCreated.Text = grant.CreatedDate.ToShortDateString();
            this.lblAmount.Text = String.Format( "{0:c}", grant.Value );
            this.lblPaymentMethod.Text = grant.PaymentType.DisplayString;
            this.lblCheckNumber.Text = grant.CheckNumber.ToString();
            this.lblNameOnCreditCard.Text = grant.NameOnCreditCard;
            this.lblDeliveryMethod.Text = grant.DeliveryMethod.DisplayString;
            if( grant.Applicant != null )
            {
                this.lblApplicantFirstName.Text = grant.Applicant.FirstName;
                this.lblApplicantLastName.Text = grant.Applicant.LastName;
                //this.lblDateApplicationReceived.Text = 
                if ( grant.Applicant.Relationships.Count > 0 )
                {
                    this.lblRelationshipToServiceMember.Text = ( (Relationship)grant.Applicant.Relationships[0] ).TypeName;
                }
                if ( grant.Applicant.Address != null )
                {
                    this.lblApplicantStreetAddress1.Text = grant.Applicant.Address.DeliveryAddress;
                    this.lblApplicantStreetAddress2.Text = grant.Applicant.Address.SecondaryUnit;
                    this.lblCity.Text = grant.Applicant.Address.City;
                    this.lblState.Text = grant.Applicant.Address.State;
                    this.lblPostalCode.Text = grant.Applicant.Address.ZIP;
                }
                this.lblEmailAddress.Text = ( grant.Applicant.EmailAddress != null ) ? grant.Applicant.EmailAddress.DisplayString : String.Empty;
                this.lblPrimaryPhoneNumber.Text = ( grant.Applicant.PrimaryPhoneNumber != null ) ? grant.Applicant.PrimaryPhoneNumber.DisplayString : String.Empty;
                this.lblSecondaryPhoneNumber.Text = ( grant.Applicant.SecondaryPhoneNumber != null ) ? grant.Applicant.SecondaryPhoneNumber.DisplayString : String.Empty;
            }
            if ( grant.Custodian != null )
            {
                this.lblCustodianFirstName.Text = grant.Custodian.FirstName;
                this.lblCustodianLastName.Text = grant.Custodian.LastName;
                Address custodianAddress = grant.Custodian.AddressFor( TypeOfContactPoint.DESCRIPTOR_ALTERNATIVE );
                if ( custodianAddress != null )
                {
                    this.lblCustodianStreetAddress1.Text = custodianAddress.DeliveryAddress;
                    this.lblCustodianStreetAddress2.Text = custodianAddress.SecondaryUnit;
                    this.lblCustodianCity.Text = custodianAddress.City;
                    this.lblCustodianState.Text = custodianAddress.State;
                    this.lblCustodianPostalCode.Text = custodianAddress.ZIP;
                }
            }
            this.imgCopyCustodianToClipboard.Attributes["onclick"] = "CopyCustodianAddressToClipboard(); return false;";
            this.imgCopyCustodianToClipboard.Style[HtmlTextWriterStyle.Cursor] = "pointer";
            this.imgCopyApplicantToClipboard.Attributes["onclick"] = "CopyApplicantAddressToClipboard(); return false;";
            this.imgCopyApplicantToClipboard.Style[HtmlTextWriterStyle.Cursor] = "pointer";
        }
        public override void UpdateModel()
        {
            base.UpdateModel();
        }
        #endregion

        #region Properties
        #endregion

        #region Private Methods
        #endregion

        #region Private Properties
        private Grant Grant
        {
            get
            {
                return (Grant)this.Model;
            }
        }
        #endregion

        #region Construction and Finalization
        #endregion

        #region Data Elements
        #endregion
    }
}

